/*
 * comlan
 */

package com.promfy.bughunter.domain;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.location.Address;
import android.media.AudioManager;
import android.media.SoundPool;

public class KindManager {
	public  static final int BUGS_TOTAL_COUNT = 1; 
	public  static final int BUG_KIND_PIAOCHONG = 0; 
	
	private List<CommonBug> bugList = new LinkedList<CommonBug>();
	private Context context;
	private Canvas canvas;
	private int screenW;
	private int screenH;
	private SoundPool pool;
	Map<String, Integer> soundMap = new HashMap<String, Integer>();
	private static KindManager kindManager;
	
	public List<CommonBug> getBugList() {
		return bugList;
	}

	public void setBugList(List<CommonBug> bugList) {
		this.bugList = bugList;
	}

//	public static KindManager getInstance(Context context, Canvas canvas, int screenW, int screenH){
//		if(kindManager == null){
//			kindManager = new KindManager(context, canvas, screenW, screenH);
//		
//		}
//		return kindManager;
//	}

	public KindManager(Context context, Canvas canvas, int screenW, int screenH) {
		super();
		this.context = context;
		this.canvas = canvas;
		this.screenW = screenW;
		this.screenH = screenH;
	}

	public void addBugRandom(){
		Random r = new Random();
		int key = r.nextInt(BUGS_TOTAL_COUNT);
		switch(key){
		case 0:
			KindPiaoChong kpc = new KindPiaoChong(context, canvas, screenW, screenH);
			kpc.born(getRandomStartPoint(screenW, screenH));
			bugList.add(kpc);
			break;
		case 1:
			KindZhangLang kzl = new KindZhangLang(context, canvas, screenW, screenH);
			kzl.born(kzl.getCurPoint());
			bugList.add(kzl);
			break;
		case 2:
			
			break;
		case 3:
			
			break;
		case 4:
			
			break;
		}
	}

	private PointF getRandomStartPoint(int w, int h){
		PointF p = new PointF();
		Random random = new Random();
		int xOrY = random.nextInt(4);
		int x = random.nextInt(w-10)+5;
		int y = random.nextInt(h-10)+5;
		switch (xOrY) {
		case 0:
			p.x = 0;
			p.y = y;
			break;
		case 1:
			p.x = w;
			p.y = y;
			break;
		case 2:
			p.x = x;
			p.y = 0;
			break;
		case 3:
			p.x = x;
			p.y = h;
			break;

		default:
			break;
		}
//		LogUtils.LogD("start", "p  xOrY = " + xOrY + "     x = "+p.x + "     p y = " + p.y);

		return p;
	}
	
	public List<CommonBug> createBugsRandom(int num) {
		bugList.clear();
		for(int i = 0; i< num ; i ++){
			addBugRandom();
		}
		return bugList;
	}
	
	/**
	 * init sound pool, for playing audio.
	 * you should load new res into soundMap, if you need add new source.
	 */
	private void initSound() {
		pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 5);  
		AssetFileDescriptor afd;
		try {
			afd = context.getAssets().openFd("name.ogg");
			soundMap.put("name.ogg", pool.load(context, 1, 1)); 
	 		 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
